package MainUI;


import java.io.FileNotFoundException;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.*;
import javafx.stage.Stage;

import static javafx.stage.StageStyle.TRANSPARENT;

public class MainUI extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        //去掉系统自带的标题栏
        primaryStage.initStyle(TRANSPARENT);
        //建立主面板
        BorderPane borderPane = new BorderPane();
        //初始化、建立自定义的标题栏
        TitleBlock titleBlock = new TitleBlock(primaryStage);
        //初始化、建立目录树视图
        DirectoryTreeUI directoryTreeUI = new DirectoryTreeUI();
        //初始化、建立预览视图（包括上信息栏）
        PreviewInterfaceUI previewInterfaceUI = new PreviewInterfaceUI();
        //初始化、建立下信息栏栏视图
        LowerColumnUI lowerColumnUI = new LowerColumnUI();
        //开始安置各视图进入主面板
        borderPane.setLeft(directoryTreeUI.getStackPane());
        borderPane.setCenter(previewInterfaceUI.getInterfaceUI());
        borderPane.setBottom(lowerColumnUI.getBorderPaneLowerColumn());
        borderPane.setTop(titleBlock.getTitleBlock());
        //加载画面
        Scene scene = new Scene(borderPane, 900, 700);
        primaryStage.setScene(scene);
        primaryStage.show();
        primaryStage.setOnCloseRequest(e -> {
            System.exit(0);
        });

        try {
            PreviewInterfaceUI.reFlesh(DirectoryTreeUI.getTempFile());
            borderPane.setOnKeyPressed(e -> {
                if (e.getCode() == KeyCode.CONTROL) {
                    PreviewInterfaceUI.setMultipleChoice(true);
                    PreviewInterfaceUI.setMultipleChoiceEnding(false);
                    System.out.println("开始");
                    PreviewInterfaceUI.setChoicePictureNum(0);
                }
                if (PreviewInterfaceUI.isMultipleChoice()) {
                    if (e.getCode() == KeyCode.A) {
                        previewInterfaceUI.contextMenu.hide();
                        System.out.println("快捷全选");
                        if (PreviewInterfaceUI.getArrayListImageFileChoice().size() == PreviewInterfaceUI.getNumOfImage()) {
                            PreviewInterfaceUI.cleanArrayChoices();
                            PreviewInterfaceUI.cleanImageStyle();
                            PreviewInterfaceUI.initFlag();
                            LowerColumnUI.setLabelDataInformationText();
                            PreviewInterfaceUI.setMultipleChoiceEnding(true);
                        } else {
                            PreviewInterfaceUI.selectAllPicture();
                        }
                    } else if (e.getCode() == KeyCode.C) {
                        previewInterfaceUI.contextMenu.hide();
                        System.out.println("快捷复制");
                        PreviewInterfaceUI.copyPicture();
                        PreviewInterfaceUI.setMultipleChoiceEnding(true);
                    } else if (e.getCode() == KeyCode.V) {
                        previewInterfaceUI.contextMenu.hide();
                        System.out.println("快捷粘贴");
                        PreviewInterfaceUI.pastePicture();
                        PreviewInterfaceUI.setMultipleChoiceEnding(true);
                    } else if (e.getCode() == KeyCode.D) {
                        previewInterfaceUI.contextMenu.hide();
                        System.out.println("快捷删除");
                        PreviewInterfaceUI.deletePicture();
                        PreviewInterfaceUI.setMultipleChoiceEnding(true);
                        PreviewInterfaceUI.setMultipleChoice(false);
                        System.out.println("结束");
                    } else if (e.getCode() == KeyCode.X) {
                        previewInterfaceUI.contextMenu.hide();
                        System.out.println("快捷剪切");
                        PreviewInterfaceUI.sharePicture();
                        PreviewInterfaceUI.setMultipleChoiceEnding(true);
                    } else if (e.getCode() == KeyCode.P) {
                        previewInterfaceUI.contextMenu.hide();
                        System.out.println("快捷播放");
                        PreviewInterfaceUI.slidePlay();
                        PreviewInterfaceUI.setMultipleChoiceEnding(true);
                    }
                } else {
                    if (e.getCode() == KeyCode.F5) {
                        previewInterfaceUI.contextMenu.hide();
                        System.out.println("快捷刷新");
                        if (DirectoryTreeUI.getTempFile() != null) {
                            try {
                                PreviewInterfaceUI.reFlesh(DirectoryTreeUI.getTempFile());
                            } catch (FileNotFoundException ex) {
                                ex.printStackTrace();
                            }
                        }
                    }
                }
                borderPane.requestFocus();
            });

            borderPane.setOnKeyReleased(e -> {
                if (e.getCode() == KeyCode.CONTROL) {
                    PreviewInterfaceUI.setMultipleChoice(false);
                    System.out.println("结束");
                }
                borderPane.requestFocus();
            });
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
